package com.thoughtworks.android.androsphere.controllers;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.thoughtworks.android.androsphere.model.CloudRequest;
import com.thoughtworks.android.androsphere.service.c2dm.C2DMService;
import com.thoughtworks.android.androsphere.service.registration.RegistrationService;

@Controller
public class C2DMController {
    private RegistrationService registrationService;
    private C2DMService c2dmService;

    @Autowired
    public C2DMController(RegistrationService registrationService, C2DMService c2dmService) {
        this.registrationService = registrationService;
        this.c2dmService = c2dmService;
    }

    @RequestMapping("/broadcast")
    public void broadcast(@RequestBody CloudRequest cloudRequest, HttpServletResponse response) {
        List<String> peerRegistrations = registrationService.peerRegistrationsFor(cloudRequest.getDevice());
        c2dmService.push(peerRegistrations, cloudRequest);
    }
}
